package FlowGram.Application.IDEWindow.FileHost;

import javax.swing.event.DocumentEvent;
import javax.swing.event.DocumentListener;

import FlowGram.Application.Parser.FinchParser;
import FlowGram.Application.Parser.FinchParserException;

public class MyDocumentListener implements DocumentListener {
	private FileHost fileHost;
	
	public MyDocumentListener(FileHost newFileHost){
		this.fileHost = newFileHost;
	}
    public void insertUpdate(DocumentEvent e) {
    	if(this.fileHost.getOpenFiles().size()!=0){
	    	CodeEditor codeEditor = (CodeEditor)fileHost.getCurrentCodeFile().getFileTabbedPane().getComponentAt(0);
	    	this.fileHost.getCurrentCodeFile().setDirty(true);
	    	FinchParser parser = new FinchParser();
	    	try {
	    		this.fileHost.getCurrentCodeFile().setProgramFlow(parser.Parse(codeEditor.getCodeEditorPane().getText()));
				//FlowEditor flowEditor = (FlowEditor)currentCodeFile.fileTabbedPane.getComponentAt(1);
				//flowEditor.setUpFlowEditor(currentCodeFile.programFlow);
			} catch (FinchParserException e1) {
				// TODO Auto-generated catch block
				//e1.printStackTrace();
			}
    	}
    }
    public void removeUpdate(DocumentEvent e) {
    	if(this.fileHost.getOpenFiles().size()!=0){
	    	CodeEditor codeEditor = (CodeEditor)this.fileHost.getCurrentCodeFile().getFileTabbedPane().getComponentAt(0);
	    	this.fileHost.getCurrentCodeFile().setDirty(true);
	    	FinchParser parser = new FinchParser();
	    	try {
	    		this.fileHost.getCurrentCodeFile().setProgramFlow(parser.Parse(codeEditor.getCodeEditorPane().getText()));
				//FlowEditor flowEditor = (FlowEditor)currentCodeFile.fileTabbedPane.getComponentAt(1);
				//flowEditor.setUpFlowEditor(currentCodeFile.programFlow);
			} catch (FinchParserException e1) {
				// TODO Auto-generated catch block
				//e1.printStackTrace();
			}
    	}
    }
    public void changedUpdate(DocumentEvent e) {
    	if(fileHost.getOpenFiles().size()!=0){
	    	CodeEditor codeEditor = (CodeEditor)this.fileHost.getCurrentCodeFile().getFileTabbedPane().getComponentAt(0);
	    	this.fileHost.getCurrentCodeFile().setDirty(true);
	    	FinchParser parser = new FinchParser();
	    	try {
	    		this.fileHost.getCurrentCodeFile().setProgramFlow(parser.Parse(codeEditor.getCodeEditorPane().getText()));
				//FlowEditor flowEditor = (FlowEditor)currentCodeFile.fileTabbedPane.getComponentAt(1);
				//flowEditor.setUpFlowEditor(currentCodeFile.programFlow);
			} catch (FinchParserException e1) {
				//e1.printStackTrace();
			}
    	}
    }
}